package com.sk.bilibili.config;

import com.sk.api.annotations.Bean;
import com.sk.bilibili.other.BiliBiliProperty;
import com.sk.bilibili.other.SessionInterceptor;
import com.sk.bilibili.utils.PreferenceUtils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetConfig {

    @Bean("client")
    public OkHttpClient okHttpClient(BiliBiliProperty property, PreferenceUtils preferenceUtils){
        return new OkHttpClient.Builder()
                .addInterceptor(new SessionInterceptor(preferenceUtils.getString("bSessionId")))
                .connectTimeout(property.getInt("connectTimeout"), TimeUnit.MILLISECONDS)
                .build();
    }

    @Bean
    public Retrofit retrofit(OkHttpClient client, BiliBiliProperty property){
        return new Retrofit.Builder()
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(property.getString("baseUrl"))
                .build();
    }

}